# encoding=utf-8
import unittest, os
from selenium import webdriver
import win32api, win32con
from Parents.Base.BaseDefault import BaseDefault


class Base(unittest.TestCase, BaseDefault):
    """
    所有测试页面的总父类，初始化浏览器和设定等待时间
    """

    def setUp(self):
        """
        初始化驱动、浏览器、默认加载时长、窗口大小等信息
        :return:
        """
        if not os.path.exists(self.defaultdownpath):
            os.makedirs(self.defaultdownpath)
        profile = webdriver.FirefoxProfile()
        profile.set_preference("browser.download.folderList", 2)
        profile.set_preference("browser.download.dir", self.defaultdownpath)
        profile.set_preference("browser.helperApps.neverAsk.saveToDisk",
                               "application/vnd.ms-excel,application/msexcel,application/octet-stream,text/csv")

        '''
        self.driver.profile.set_preference("browser.download.dir", defaultdownpath)
        self.driver.profile.setPreference("browser.download.folderList", 2)
        self.driver.profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
                                          "application/octet-stream, application/vnd.ms-excel, text/csv, application/zip,application/msexcel");
        '''
        try:
            self.driver = webdriver.Firefox(profile)
        except Exception as e:
            print("尝试第二次加载驱动")
            self.driver = webdriver.Firefox(profile)
        self.currentBrowser = 'firefox'
        self.driver.implicitly_wait(30)
        self.driver.set_page_load_timeout(30)
        self.driver.set_script_timeout(30)
        self.driver.maximize_window()
        self.driver.set_window_size(win32api.GetSystemMetrics(win32con.SM_CXSCREEN),
                                    win32api.GetSystemMetrics(win32con.SM_CYSCREEN))
        self.driver.delete_all_cookies()

    def tearDown(self):
        """
        关闭驱动
        :return:
        """
        self.driver.quit()
